package vip.meeet.collection.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ListRemove {

    public static void main(String[] args) {
//        printAdd();
        printtest();
    }

    private static void print(List<Integer> list,int index){
        if(list != null && index>=0&&list.size()>index){
            Integer value = list.remove(index);
            System.out.print(value);
            print(list,value % 3);
        }
    }

    private static void printtest(){
        List<Integer> integers = new LinkedList<>();
        for(int i=1;i<10;i++){
            integers.add(i);
        }
        print(integers,2);
    }

    private static void printAdd() {
//        List<String> list = new ArrayList<>();
        List<String> list = new LinkedList<>();
        list.add("a");
        list.add("b");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
            list.add("c");
        }
    }

    public static void wrongRemove() {
        List<String> platformList = new ArrayList<>();
        platformList.add("博客园");
        platformList.add("CSDN");
        platformList.add("掘金");

        for (String platform : platformList) {
            if (platform.equals("博客园")) {
                platformList.remove(platform);
            }
        }
        System.out.println(platformList);
    }

    public static void iteratorRemove() {
        List<String> platformList = new ArrayList<>();
        platformList.add("博客园");
        platformList.add("CSDN");
        platformList.add("掘金");

        Iterator<String> iterator = platformList.iterator();
        while (iterator.hasNext()) {
            String platform = iterator.next();
            if (platform.equals("博客园")) {
                iterator.remove();
            }
        }
        System.out.println(platformList);
    }

    public static void forfowardRemove() {
        List<String> platformList = new ArrayList<>();
        platformList.add("博客园");
        platformList.add("CSDN");
        platformList.add("掘金");

        for (int i = 0; i < platformList.size(); i++) {
            String item = platformList.get(i);
            if (item.equals("博客园")) {
                platformList.remove(i);
                i = i - 1;//删除之后下标变化了
            }
        }
        System.out.println(platformList);
    }

    public static void forbackwardRemove() {
        List<String> platformList = new ArrayList<>();
        platformList.add("博客园");
        platformList.add("CSDN");
        platformList.add("掘金");

        for (int i = platformList.size() - 1; i >= 0; i--) {
            String item = platformList.get(i);
            if (item.equals("掘金")) {
                platformList.remove(i);
            }
        }
        System.out.println(platformList);
    }
}
